package com.hectopascal.goalance.controller;

import com.hectopascal.goalance.pojo.User;
import com.hectopascal.goalance.pojo.dto.ResponseMessage;
import com.hectopascal.goalance.pojo.dto.UserAdd;
import com.hectopascal.goalance.pojo.dto.UserBack;
import com.hectopascal.goalance.service.Ipml.IUserService;
import com.hectopascal.goalance.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController  //接口方法返回对象，转换成json版本
@RequestMapping("/user")
public class UserController {

    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    IUserService userService;

    @PostMapping("/addpeople")
    public ResponseMessage<User> userAdd (@RequestBody UserAdd userAdd) {
        User user=userService.userAdd(userAdd);
        return ResponseMessage.success(user);
    }

    @GetMapping("/getInfo/{userId}")
    public ResponseMessage<UserBack> userGet(@PathVariable Integer userId) {
        UserBack user=userService.getUser(userId);
        return ResponseMessage.success(user);
    }

    @PostMapping("/login")
    public ResponseMessage<Map<String,Object>> userLogin(@RequestBody User user) {
        UserBack userBack=userService.userLogin(user);
        String token=jwtUtils.generateToken(userBack.getUsername(),userBack.getRole(), userBack.getId());
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("user", userBack);
        responseData.put("token", token);
        return ResponseMessage.success(responseData);
    }

    @PostMapping("/verifyPassword")
    public ResponseMessage<Boolean> verifyPassword(@RequestBody User user,HttpServletRequest request) {
        Integer userId=(Integer)request.getAttribute("userId");
        user.setId(userId);
        boolean result=userService.verifyPassword(user);
        return ResponseMessage.success(result);
    }

    @PutMapping("/update")
    public ResponseMessage<String> userUpdate(@RequestBody User user) {
        int row=userService.userUpdate(user);
        if(row!=0){
            return  ResponseMessage.success("成功修改数据");
        }else{
            return ResponseMessage.fail("修改数据失败");
        }
    }

    @DeleteMapping("/delete/{userId}")
    public ResponseMessage<String> userDelete(@PathVariable Integer userId){
        int row = userService.userDelete(userId);
        if(row!=0){
            return  ResponseMessage.success("成功删除数据");
        }else{
            return ResponseMessage.fail("删除数据失败");
        }
    }

    @GetMapping("/getUsersInfo")
    public ResponseMessage<Map<String, Object>> getUsersInfo(@RequestParam Integer page,
                                                        @RequestParam Integer size,
                                                        @RequestParam(required = false) String role,
                                                        @RequestParam(required = false) Integer departmentId,
                                                        @RequestParam(required = false) String username) {
        return ResponseMessage.success(userService.getUsersInfo(page,size,role,departmentId,username));
    }

    @GetMapping("/getEmployeeInfo")
    public ResponseMessage<List<UserBack>> getRoleInfo(HttpServletRequest request) {
        Integer userId=(Integer)request.getAttribute("userId");
        return ResponseMessage.success(userService.getEmployeeInfo(userId));
    }

    @PutMapping("/update/avatar")
    public ResponseMessage<String> userUpdateAvatar(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        try{
            Integer userId=(Integer)request.getAttribute("userId");
            String avatarUrl=userService.userUpdateAvatar(file,userId);
            return new ResponseMessage(200,"上传成功",avatarUrl);
        }catch(IOException e){
            return ResponseMessage.fail("上传失败");
        }
    }
}
